/*
 * 文件名：CustManaTransactionImp.java
 * 版权：Copyright by www.wootide.com
 * 描述：
 * 修改人：Administrator
 * 修改时间：2013-2-21
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.service.customer.custmanager.transaction.imp;

import org.springframework.beans.BeanUtils;

import com.wootide.cms.common.CMSRuntimeException;
import com.wootide.cms.common.ErrorCodes;
import com.wootide.cms.common.Page;
import com.wootide.cms.common.QueryCondition;
import com.wootide.cms.common.QueryResults;
import com.wootide.cms.common.dao.CommonDao;
import com.wootide.cms.log.syslog.RunLog;
import com.wootide.cms.service.customer.custmanager.dao.CustManagerDao;
import com.wootide.cms.service.customer.custmanager.pojo.ClientLevel;
import com.wootide.cms.service.customer.custmanager.pojo.Customer;
import com.wootide.cms.service.customer.custmanager.transaction.CustManagerTransaction;

public class CustManagerTransactionImp implements CustManagerTransaction
{
    
    // 注入运行日志
    private RunLog runLog;
    
    //公共数据访问层实例
    private CommonDao commonDao;
    
    //用户数据访问层实例
    private CustManagerDao custManagerDao;
    
    @Override
    public void addCustomer(Customer customer)
    {
        // 打印方法入口参数
        runLog.debug("customer=" + customer);
        // 根据用户名检查唯一性
        Integer num = custManagerDao.findCustomerNumByCardNo(customer.getCardNo());
        //用户记录已经存在
        if (num > 0)
        {
            throw new CMSRuntimeException("User Existed!",
                    ErrorCodes.Sys.User.USEREXIST);
        }
        commonDao.addOneObject(customer);
        
    }
    
    @Override
    public QueryResults queryForPages(Page page, QueryCondition queryConditon)
    {
        runLog.debug("page=" + page);
        runLog.debug("queryConditon=" + queryConditon);
        QueryResults queryResults = custManagerDao.queryForPages(page, queryConditon);
        return queryResults;
    }
    
    public RunLog getRunLog()
    {
        return runLog;
    }
    
    public void setRunLog(RunLog runLog)
    {
        this.runLog = runLog;
    }
    
    public CommonDao getCommonDao()
    {
        return commonDao;
    }
    
    public void setCommonDao(CommonDao commonDao)
    {
        this.commonDao = commonDao;
    }
    
    public CustManagerDao getCustManagerDao()
    {
        return custManagerDao;
    }
    
    public void setCustManagerDao(CustManagerDao custManagerDao)
    {
        this.custManagerDao = custManagerDao;
    }

    @Override
    public Customer viewOneCustomer(Integer innerIndex)
    {
        // 打印方法入口参数
        runLog.debug("innerIndex=" + innerIndex);
        Customer customerByInfo = custManagerDao.findOneCustomerByCustomerById(innerIndex);
        return customerByInfo;
    }

    @Override
    public QueryResults queryCustomerLevel(QueryCondition queryConditon)
    {
        runLog.debug("queryConditon=" + queryConditon);
        QueryResults queryResults = custManagerDao.queryCustomerLevel(queryConditon);
        return queryResults;
    }

    @Override
    public void addClientLevel(ClientLevel clientLevel)
    {
        // 打印方法入口参数
        runLog.debug("clientLevel=" + clientLevel);
        /*// 根据用户名检查唯一性
        Integer num = custManagerDao.findNumByUserName(customer.getUserName());
        //用户记录已经存在
        if (num > 0)
        {
            throw new CMSRuntimeException("User Existed!",
                    ErrorCodes.Sys.User.USEREXIST);
        }*/
        
        commonDao.addOneObject(clientLevel);
        
    }

    @Override
    public Customer delOneCustomer(Integer innerIndex)
    {
        Customer oldCustomerInfo = custManagerDao.findCustomerByCustomerId(innerIndex);
        //判断要删除的记录是否存在
        if (null == oldCustomerInfo)
        {
            throw new CMSRuntimeException("CustomerInfo not Exist!",
                    ErrorCodes.Reservation.OrderBy.ORDERBYNOTEXIST);
        }
        Customer oldCustomerCopy = new Customer();
        BeanUtils.copyProperties(oldCustomerInfo, oldCustomerCopy);
        
        //修改用户信息
        oldCustomerInfo.setStatus(2);
        //commonDao.delOneObject(oldCustomerInfo);
        //        BeanUtils.copyProperties(oldUserInfo, oldUserInfoCopy);
        return oldCustomerInfo;
    }

    @Override
    public Customer modOneCustomer(Customer customer)
    {
        // 打印方法入口参数
        runLog.debug("Customer=" + customer);
        Customer oldCustomer = custManagerDao.findOneCustomerByCustomerId(customer.getCustomerId());
        
        //判断要修改的记录是否存在
        if (null == oldCustomer)
        {
            throw new CMSRuntimeException("Customer not Exist!",
                    ErrorCodes.Reservation.OrderBy.ORDERBYNOTEXIST);
        }
        
        //获取原始数据副本
        Customer oldCustomerCopy = new Customer();
        BeanUtils.copyProperties(oldCustomer, oldCustomerCopy);
        
        //修改用户信息
        oldCustomer.setCardNo(customer.getCardNo());
        oldCustomer.setCardSn(customer.getCardSn());
        oldCustomer.setCustName(customer.getCustName());
        oldCustomer.setClientLevelId(customer.getClientLevelId());
        oldCustomer.setSex(customer.getSex());
        if(customer.getPhotoPath() != null && !"".equals(customer.getPhotoPath())){
            oldCustomer.setPhotoPath(customer.getPhotoPath());
        }
        if(customer.getRegistrationDate() != null && !"".equals(customer.getRegistrationDate())){
            oldCustomer.setRegistrationDate(customer.getRegistrationDate());
        }
        if(customer.getBirthday() !=null && !"".equals(customer.getBirthday())){
            oldCustomer.setBirthday(customer.getBirthday());
        }
        oldCustomer.setStatus(customer.getStatus());
        oldCustomer.setTypeID(customer.getTypeID());
        oldCustomer.setNumberID(customer.getNumberID());
        oldCustomer.setTel(customer.getTel());
        oldCustomer.setProfession(customer.getProfession());
        //oldCustomer.setPersonalHobby(customer.getPersonalHobby());
        
        return oldCustomerCopy;
    }

    @Override
    public ClientLevel viewOneLevel(Integer innerIndex)
    {
        // 打印方法入口参数
        runLog.debug("innerIndex=" + innerIndex);
        ClientLevel clientLevelInfo = custManagerDao.findOneLevelByClientLevelId(innerIndex);
        return clientLevelInfo;
    }

    @Override
    public ClientLevel modOneLevel(ClientLevel clientLevel)
    {
        // 打印方法入口参数
        runLog.debug("clientLevel=" + clientLevel);
        ClientLevel oldClientLevel = custManagerDao.findOneLevelByClientLevelId(clientLevel.getClientLevelID());
        
        //判断要修改的记录是否存在
        if (null == oldClientLevel)
        {
            throw new CMSRuntimeException("clientLevel not Exist!",
                    ErrorCodes.Reservation.OrderBy.ORDERBYNOTEXIST);
        }
        
        //获取原始数据副本
        ClientLevel oldClientLevelCopy = new ClientLevel();
        BeanUtils.copyProperties(oldClientLevel, oldClientLevelCopy);
        
        //修改用户信息
        oldClientLevel.setClientLevelName(clientLevel.getClientLevelName());
        oldClientLevel.setDiscount(clientLevel.getDiscount());
        oldClientLevel.setLongestRetention(clientLevel.getLongestRetention());
        oldClientLevel.setClientLevelDesc(clientLevel.getClientLevelDesc());
        
        return oldClientLevelCopy;
    }

    @Override
    public ClientLevel delOneClientLevel(Integer innerIndex)
    {
        ClientLevel oldLevelInfo = custManagerDao.findOneLevelByClientLevelId(innerIndex);
        //判断要删除的记录是否存在
        if (null == oldLevelInfo)
        {
            throw new CMSRuntimeException("ClientLevel not Exist!",
                    ErrorCodes.Reservation.OrderBy.ORDERBYNOTEXIST);
        }
        
        commonDao.delOneObject(oldLevelInfo);
        return oldLevelInfo;
    }

    @Override
    public Customer lostOneCustomer(Customer customer)
    {
        // 打印方法入口参数
        runLog.debug("Customer=" + customer);
        Customer oldCustomer = custManagerDao.findOneCustomerByCustomerId(customer.getCustomerId());
        
        //判断要修改的记录是否存在
        if (null == oldCustomer)
        {
            throw new CMSRuntimeException("Customer not Exist!",
                    ErrorCodes.Reservation.OrderBy.ORDERBYNOTEXIST);
        }
        
        //获取原始数据副本
        Customer oldCustomerCopy = new Customer();
        BeanUtils.copyProperties(oldCustomer, oldCustomerCopy);
        
        //修改用户信息
        oldCustomer.setStatus(customer.getStatus());
        
        return oldCustomerCopy;
    }

    @Override
    public QueryResults queryLevelForPages(Page page,
            QueryCondition queryConditon)
    {
        runLog.debug("page=" + page);
        runLog.debug("queryConditon=" + queryConditon);
        QueryResults queryResults = commonDao.queryForPages(page, queryConditon);
        return queryResults;
    }
    
}
